package com.ns.dcjh.ui.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.animation.LinearInterpolator;
import android.widget.LinearLayout;
import android.widget.Scroller;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.ns.dcjh.R;

public class MyScrollLinearLayout extends LinearLayout {

    private Scroller mScroller;

    private int mContentTopDistance = 1500; //子View离屏幕上边距离

    private OnScrollCompleteListener onScrollCompleteListener;

    private int pauseDyDiffHeight = 0;


    public MyScrollLinearLayout(@NonNull Context context) {
        this(context, null);
    }

    public MyScrollLinearLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyScrollLinearLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }


    private void init(Context mContext) {
        mScroller = new Scroller(mContext, new LinearInterpolator());
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //ViewGroup主要是一个容器，当ViewGroup的宽高是确切的值的时候，控件的宽高就是它本身设置的值
        //主要是考虑ViewGroup Wrap_content的时，需要计算控件的宽高，控件的宽高根据子View的布局来计算
        int width;
        int height;
        measureChildren(widthMeasureSpec, heightMeasureSpec); // 测量一下子控件的宽高
        width = MeasureSpec.getSize(widthMeasureSpec);
//        TextView childView= null;
//        for (int i=0;i<getChildCount();i++){
//            if (getChildAt(i).getId() == R.id.displayContent){
//                childView= (TextView) getChildAt(i);
//                int contentHeight = childView.getLayout().getHeight()  //  文字的高度
//                        + childView.getPaddingTop()
//                        + childView.getPaddingBottom();
//                Log.e("TAG", "onMeasure height="+contentHeight);
//            }
//        }

        height = MeasureSpec.getSize(heightMeasureSpec);

        setMeasuredDimension(width, height);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
//        super.onLayout(changed, l, t, r, b);
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            TextView view = (TextView) getChildAt(i);
            if (view.getId() == R.id.displayContent) {
                int width = view.getMeasuredWidth();
                int p_width = getWidth();
                int p_height = getHeight();

                int height = view.getLayout().getHeight()  //  文字的高度
                        + view.getPaddingTop()
                        + view.getPaddingBottom();

                int distance = (p_width - width) / 2;

                Log.e("TAG", "onLayout 长宽分别为(" + width + "," + height + ")");
                view.layout(distance, mContentTopDistance, width + distance, mContentTopDistance + height);

            }
        }
    }

    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()) {
//            Log.e("SCROLL","("+mScroller.getCurrX()+","+mScroller.getCurrY()+")");
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            postInvalidate();
        } else {
            onScrollCompleteListener.onScrollComplete(mScroller.getCurrY());
        }
    }

    public int getCurrY() {
        return mScroller.getCurrY();
    }

    /**
     * 调用此方法设置滚动的相对偏移
     *
     * @param dy     滚动量
     * @param startY 启动y的位置
     */
    public void startScroll(int dy, int startY, float speed) {

        //设置mScroller的滚动偏移量
        mScroller.startScroll(mScroller.getFinalX(), startY, 0, dy, (int) (dy / speed));
        invalidate();//这里必须调用invalidate()才能保证computeScroll()会被调用，否则不一定会刷新界面，看不到滚动效果
    }

    public void initScrollY(int dy) {
        scrollTo(0, dy);
        postInvalidate();
    }


    public void stopScroll() {
        mScroller.forceFinished(true);
    }


    public void setContentTopDistance(int mContentTopDistance) {
        this.mContentTopDistance = mContentTopDistance;
    }

    public void setOnScrollCompleteListener(OnScrollCompleteListener onScrollCompleteListener) {
        this.onScrollCompleteListener = onScrollCompleteListener;
    }


}
